package com.yongqi.duocai.controller;


import com.yongqi.duocai.entity.User;
import com.yongqi.duocai.entity.result.UserInfoResult;
import com.yongqi.duocai.exception.BusinessException;
import com.yongqi.duocai.exception.ResponseEnum;
import com.yongqi.duocai.service.UserService;
import com.yongqi.duocai.utils.JsonResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 夏亮辉
 * @since 2021-06-22
 */
@RestController
@RequestMapping("/duocai/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/getUserInfo/{userId}")
    @ApiOperation("根据id获取用户信息")
    public JsonResult getUserInfo(@PathVariable(value = "userId") Integer userId){
        JsonResult jsonResult = new JsonResult();
        User user = userService.getById(userId);
        UserInfoResult result = new UserInfoResult();
        if(user != null){
            // 组装数据
            BeanUtils.copyProperties(user,result);
            jsonResult.setCode(200);
            jsonResult.setData(result);
        }else {
            throw new BusinessException(ResponseEnum.LOGIN_AUTH_ERROR);
        }
        return jsonResult;
    }

}

